const sql = require('mssql');
const baseFn = require('../../public/base.js');

/**
 *  拣货统计接口 
 */

// 获取拣货人每天的订单量
const getPickStatisticsInfo = async (ctx, next) => {
    let getBody = ctx.request.body;
    let selectTime = getBody.searchList.selectTime;
    // 每天的数据的集合
    let dayList = [];
    if (selectTime) {
        let formatTime = new Date(selectTime);
        let year = formatTime.getFullYear();
        let month = formatTime.getMonth() + 1
        let days = new Date(year, month, 0).getDate();
        for (let day = 1; day <= days; day++) {
            let selectDaySql = `SELECT T2.userid,T2.shopCount,T2.MC,T3.orderCount FROM (

                (SELECT Y.userid,COUNT(Y.YPBM) AS shopCount,G.MC FROM   YHDD Y LEFT JOIN BM_YG G ON Y.userid = G.BM  WHERE   JHRQ between '${selectTime + "-" + day} 00:00:01' and '${selectTime + "-" + day} 23:59:59' AND Y.JHZT = 'T'    GROUP BY Y.userid,G.MC) AS T2 LEFT JOIN 
                
                (SELECT COUNT(T1.id) AS orderCount,T1.userid FROM (SELECT userid,id FROM YHDD  WHERE   JHRQ between '${selectTime + "-" + day} 00:00:01' and '${selectTime + "-" + day} 23:59:59' AND JHZT = 'T'  GROUP BY userid,id) T1 GROUP BY T1.userid) T3 ON T2.userid = T3.userid)`
            let dayData = await sql.query(selectDaySql);
            dayList.push(dayData.recordset);
        }
    }

    ctx.body = { result: dayList };
}

module.exports = {
    'POST /getPickStatisticsInfo': getPickStatisticsInfo,
}